//
//  JMRepayVC.m
//  NewiOSProject
//
//  Created by JMMac on 16.1.24.
//  Copyright © 2024 范智渊. All rights reserved.
//

#import "JMRepayVC.h"
#import "JMOrderHeaderV.h"
#import "JMPaymentView.h"
#import "JMRepaymentModel.h"

@interface JMRepayVC ()<JMOrderHeaderVDelegate,JMPaymentViewDelegate>
@property (nonatomic, strong)UIScrollView *mainSV;
@property (nonatomic, strong)JMOrderHeaderV *headView;
@property (nonatomic, strong)JMPaymentView *paymentView;

@property (nonatomic, strong)JMRepaymentModel *paymenModel;

@end

@implementation JMRepayVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.NavImg.backgroundColor = Hex_Str_COLOR(@"#3C57F8");
    if (self.extModel) {
        self.TitleLabel.text = @"Pagar extensión de plazo";
    }else{
        self.TitleLabel.text = @"Reembolso";
    }
    self.TitleLabel.textColor = WhiteColor();
    self.NavImg.backgroundColor = mainSchemeColor();
    [self getRepaymentDetail];
}
-(void)getRepaymentDetail{
 
    NSDictionary *param;
    if (self.extModel) {
        param = @{
            @"delayTerm":_extModel.termUnit,
            @"orderNo":_extModel.orderNo,
            @"requestType":@"2"
        };
        [JMCommonMethod uploadStatistics:@"Repayment" actionType:@"Enter" parameters:@{
            @"type": self.paymenModel.leftPayModel.payTypeString, @"loanOrderId":self.extModel?self.extModel.orderNo:self.infoModel.orderNo}];
    }else{
        param = @{
            @"delayTerm":_infoModel.termUnit,
            @"orderNo":_infoModel.orderNo,
            @"requestType":@"1"
        };
        [JMCommonMethod uploadStatistics:@"Repayment" actionType:@"Enter" parameters:@{
            @"type": self.paymenModel.leftPayModel.payTypeString, @"loanOrderId":self.extModel?self.extModel.orderNo:self.infoModel.orderNo}];
    }
    [[JMBaseRequest sharedInstance] requestBasePost:BasePostRequestUrl(@"/api/order/repayment-detail") params:param success:^(NSDictionary * _Nullable dataDic, NSArray * _Nullable dataArray) {
        [self stopAnimating];
        self.paymenModel = [JMRepaymentModel mj_objectWithKeyValues:dataDic];
        if (self.extModel) {
            self.paymenModel.extModel = self.extModel;
        }else{
            self.paymenModel.infoModel = self.infoModel;
        }
      
        [self.view addSubview:self.mainSV];

    } fail:^(NSString * _Nonnull errorMsg) {
        
    }];
}
-(UIScrollView *)mainSV{
    if (!_mainSV) {
        _mainSV = [[UIScrollView alloc]initWithFrame:(CGRectMake(0, NAVIGATION_BAR_HEIGHT(), kAppW(), kAppH()-NAVIGATION_BAR_HEIGHT()))];
        [_mainSV addSubview:self.headView];
        [_mainSV addSubview:self.paymentView];
        _mainSV.contentSize =CGSizeMake(kAppW(), _paymentView.bottom);
        _mainSV.backgroundColor = mainSchemeColor();
        if (@available(iOS 11.0, *)) {
            _mainSV.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            self.automaticallyAdjustsScrollViewInsets = false;
        }
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    return _mainSV;
}
-(JMOrderHeaderV *)headView{
    if (!_headView) {
        _headView = [[JMOrderHeaderV alloc]initWithFrame:(CGRectMake(0, 0, kAppW(), 1))];
        _headView.delegate = self;
        _headView.paymentModel = self.paymenModel;
    }
    return _headView;
}
-(void)updateHeight{
    _paymentView.top = self.headView.bottom;
    _mainSV.contentSize = CGSizeMake(kAppW(), self.paymentView.bottom);
    NSLog(@"%.f",_mainSV.contentSize.height);
}
-(JMPaymentView *)paymentView{
    if (!_paymentView) {
        _paymentView = [[JMPaymentView alloc]initWithFrame:(CGRectMake(0, self.headView.bottom, kAppW(), 1))];
        _paymentView.paymentModel = self.paymenModel;
        _paymentView.delegate = self;
    }
    return _paymentView;
}
-(void)botViewUpdateHeight{
    _paymentView.top = self.headView.bottom;
    _mainSV.contentSize = CGSizeMake(kAppW(), self.paymentView.bottom);
    NSLog(@"%.f",_mainSV.contentSize.height);

}
@end
